package com.imooc.coupon.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.imooc.coupon.constant.CouponCategory;
import com.imooc.coupon.constant.DistributeTarget;
import com.imooc.coupon.constant.ProductLine;
import com.imooc.coupon.converter.*;
import com.imooc.coupon.serialization.CouponTemplateSeralize;
import com.imooc.coupon.vo.TemplateRule;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

//优惠券模板实体类定义：基础属性+规则属性
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "coupon_template")
@JsonSerialize(using = CouponTemplateSeralize.class)
public class CouponTemplate implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",nullable = false)
    private Integer id;

    //是否是可用状态
    @Column(name = "available",nullable = false)
    private Boolean available;

    //是否过期
    @Column(name = "expired",nullable = false)
    private Boolean expired;

    //优惠券名称
    @Column(name = "name",nullable = false)
    private String name;

    //优惠券描述
    @Column(name = "intro",nullable = false)
    private String desc;


    //优惠券logo
    @Column(name = "logo",nullable = false)
    private String logo;

    //优惠券分类
    @Column(name = "category",nullable = false)
    @Convert(converter = CouponCategoryConverter.class)
    private CouponCategory category;

    //产品线
    @Column(name = "product_line",nullable = false)
    @Convert(converter = ProductLineConverter.class)
    private ProductLine productLine;

    //总数
    @Column(name = "coupon_count",nullable = false)
    private Integer count;

    //创建时间
    @CreatedDate
    @Column(name = "create_time",nullable = false)
    private Date createTime;

    //创建用户
    @Column(name = "user_id",nullable = false)
    private Long userId;

    //优惠券模板的编码
    @Column(name = "template_key",nullable = false)
    private String key;

    //目标用户
    @Column(name = "target",nullable = false)
    @Convert(converter = DistributeTargetConverter.class)
    private DistributeTarget target;

    //优惠券规则
    @Column(name = "rule",nullable = false)
    @Convert(converter = RuleConverter.class)
    private TemplateRule rule;


    //自定义构造函数
    public CouponTemplate(String name, String desc, String logo, CouponCategory category,
                          ProductLine productLine, Integer count, Long userId, DistributeTarget target, TemplateRule rule) {
        this.name = name;
        this.desc = desc;
        this.logo = logo;
        this.category = category;
        this.productLine = productLine;
        this.count = count;
        this.userId = userId;
        this.target = target;
        this.rule = rule;
    }

    public CouponTemplate(String name, String logo, String desc, String category, Integer productLine, Integer count, Long userId, Integer target, TemplateRule rule) {
    }
}
